package cn.appsys.controller.developer;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.pojo.DevUser;
import cn.appsys.service.developer.AppCategoryService;
import cn.appsys.service.developer.AppInfoService;
import cn.appsys.service.developer.DataDictionaryService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/dev/flatform/app")
public class AppController {
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private AppCategoryService appCategoryService;
    @Resource
    private DataDictionaryService dataDictionaryService;

    @RequestMapping("/list")
    public String list(
            String querySoftwareName,
            Integer queryStatus,
            Integer queryFlatformId,
            Integer queryCategoryLevel1,
            Integer queryCategoryLevel2,
            Integer queryCategoryLevel3,
            @RequestParam(defaultValue = "1") Integer pageIndex,
            HttpSession session, Model model){
        //1.获得登录用户的id
        DevUser user = (DevUser)session.getAttribute(Constants.DEV_USER_SESSION);
        try {
            //2.查询当前用户上传的app的总数量
            int totalCount = appInfoService.getAppInfoCount(
                    querySoftwareName,queryStatus,queryCategoryLevel1,
                    queryCategoryLevel2,queryCategoryLevel3,
                    queryFlatformId,user.getId());

            //3.将总数量，当前页码，每页行数，封装为PageSupport对象
            PageSupport pages = new PageSupport();
            pages.setCurrentPageNo(pageIndex);
            pages.setPageSize(5);
            pages.setTotalCount(totalCount);

            //4.查询当前用户上传的app第一页的数据list
            List<AppInfo> list = appInfoService.getAppInfoList(
                    querySoftwareName,queryStatus,queryCategoryLevel1,
                    queryCategoryLevel2,queryCategoryLevel3,
                    queryFlatformId,
                    user.getId(),pageIndex,5);
            // 5.把PageSupport对象，list保存到model
            model.addAttribute("pages",pages);
            model.addAttribute("appInfoList",list);

            //查询APP状态
            model.addAttribute("statusList",dataDictionaryService.getDataDictionaryList("APP_STATUS"));
            //查询所属平台
            model.addAttribute("flatFormList",dataDictionaryService.getDataDictionaryList("APP_FLATFORM"));
            //一级分类
            model.addAttribute("categoryLevel1List",appCategoryService.getAppCategoryListByParentId(null));
            //保存之前的查询条件
            model.addAttribute("querySoftwareName",querySoftwareName);
            model.addAttribute("queryStatus",queryStatus);
            model.addAttribute( "queryFlatformId",queryFlatformId);
            model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
            model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
            model.addAttribute( "queryCategoryLevel3",queryCategoryLevel3);
            model.addAttribute( "pageIndex",pageIndex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //6.跳转到appinfolist.jsp
        return "developer/appinfolist";
    }

    @RequestMapping("/categorylevellist.json")
    @ResponseBody
    public List<AppCategory> list( Integer pid){
        try {
            return appCategoryService.getAppCategoryListByParentId(pid);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/appinfoadd")
    public String appinfoadd(){
        return "developer/appinfoadd";
    }

    @RequestMapping("/datadictionarylist.json")
    @ResponseBody
    public List<DataDictionary> datadictionarylist(){
        try {
            return dataDictionaryService.getDataDictionaryList("APP_FLATFORM");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }




}
